-- UIInlandPayWay
-- 国内充值平台
-- create by huanghr

UIInlandPayWay = class("UIInlandPayWay", function()
    return cc.Layer:create();
end);

function UIInlandPayWay.create(goodsId)
    return UIInlandPayWay.new(goodsId);
end

-- 构造函数
function UIInlandPayWay:ctor(goodsId)
    self:setName("UIInlandPayWay");

    self.goodsId = goodsId;

    -- 初始化
    local node = cc.CSLoader:createNode("layout/market/PaymentWay.csb");
    self:addChild(node);
    self.node = node;

    if DungeonM.isInDungeon() then
        -- 迷宫中层级要调高
        self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 11);
    end

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册各种点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIInlandPayWay:registerTouchEvent()
    local node = self.node;

    -- 微信
    local CT = findChildByName(node, "CT");
    local molBtn = findChildByName(CT, "mol_btn");
    local function onMolClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- TODO:
            MarketM.buyByRmb(self.goodsId, "wechat");
            UIMgr.getCurrentScene():removeFormByName("UIInlandPayWay");
        end
    end

    molBtn:addTouchEventListener(onMolClick);
    molBtn:setTitleText("");

    -- 支付宝
    local bluepayBtn = findChildByName(CT, "bluepay_btn");
    local function onBluepayClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- TODO:
            MarketM.buyByRmb(self.goodsId, "alipay");
            UIMgr.getCurrentScene():removeFormByName("UIInlandPayWay");
        end
    end

    bluepayBtn:addTouchEventListener(onBluepayClick);
    bluepayBtn:setTitleText("");

    -- mycard 支付
    local mycardBtn = findChildByName(CT, "mycard_btn");
    mycardBtn:setVisible(false);

    -- 注册背景点击事件(点击背景，取消当前)
    local bgImg = findChildByName(self.node, "bg/bgImg");
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIInlandPayWay");
        end
    end
    bgImg:addTouchEventListener(onBgClick);
end

-- 注册事件处理回调函数
function UIInlandPayWay:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIInlandPayWay 界面析构清理");
            EventMgr.removeAll("UIInlandPayWay");
        end
    end);
end

-- 重绘
function UIInlandPayWay:redraw()
    local CT = findChildByName(self.node, "CT");

    -- 充值标题
    local titleLabel = findChildByName(CT, "title_label");

    titleLabel:setFontSize(TextStyleM.TEXT_SIZE_MIDDLE);
    titleLabel:setColor(TextStyleM.TEXT_COLOR_WHITE);

    local titleStr = getLocStr("apk_payment_method");
    titleLabel:setString(titleStr);

    -- 重新加载按钮
    self:redrawPayBtn();

    -- 把配置的支付方式加入到可视列表中，未配置的支付方式需要隐藏
    local btnList = {"mycard_btn", "bluepay_btn", "mol_btn" };
    local visibleList = {"bluepay_btn", "mol_btn"};


    local posY = 30;
    for _, btnName in ipairs(visibleList) do
        btn = findChildByName(CT, btnName);
        btn:setVisible(true);
        btn:setPosition(cc.p(0, posY));
        posY = posY - 130;
    end

    for _, btnName in ipairs(btnList) do
        if table.indexOf(visibleList, btnName) == -1 then
            btn = findChildByName(CT, btnName);
            btn:setVisible(false);
        end
    end
end

-- 重绘按钮
function UIInlandPayWay:redrawPayBtn()
    local CT = findChildByName(self.node, "CT");
    -- mol修改成微信
    local molBtn = findChildByName(CT, "mol_btn");
    molBtn:loadTextures(getInlandPayIcon("btn_wechat_charge1"), getInlandPayIcon("btn_wechat_charge2"));

    -- bluepay为支付宝
    local bluepayBtn = findChildByName(CT, "bluepay_btn");
    bluepayBtn:loadTextures(getInlandPayIcon("btn_alipay_charge1"), getInlandPayIcon("btn_alipay_charge2"));
end

-- 适配
function UIInlandPayWay:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end